package com.situ.common;

import com.mysql.cj.jdbc.Driver;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.SimpleDriverDataSource;

import javax.sql.DataSource;
import java.sql.SQLException;

public class Global {

    //驱动名称
    //连接字符串
    private static final String JDBC_URL = "jdbc:mysql://localhost:3306/mydb02?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false&allowPublicKeyRetrieval=true";
    //用户
    private static final String JDBC_USER = "root";
    //密码
    private static final String JDBC_PASSWORD = "123456";
    public static DataSource dataSource = null;

    //获取数据源 数据库连接池
    public static DataSource getDataSource() {
        if (dataSource == null) {
            try {
                Driver driver = new Driver();
                return new SimpleDriverDataSource(driver, JDBC_URL, JDBC_USER, JDBC_PASSWORD);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        } else {
            return dataSource;
        }
    }

    public static JdbcTemplate getJdbcTemplate() {
        return new JdbcTemplate(getDataSource());
    }
}
